package it.unica.informatica.enotes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import it.unica.informatica.enotes.R;
import it.unica.informatica.enotes.notes.Note;


/**
 * This class provides logic behind visualisation of ListView in MainScreenActivity
 *
 * @author Massimiliano Meles
 * @version 1.0
 */

public class NoteListAdapter extends ArrayAdapter<Note> {
   private final Context context;
   private final ArrayList<Note> values;
   private View rowView;

   /**
    * Constructor used to initialize values of Adapter
    *
    * @param context Activity context
    * @param values  ArrayList of values to insert in the ListView
    */
   public NoteListAdapter(Context context, ArrayList<Note> values) {
      super(context, R.layout.note_row_layout, values);
      this.context = context;
      this.values = values;
      rowView = null;
   }

   @Override
   public int getCount() {
      return values.size();
   }


   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      rowView = inflater.inflate(R.layout.note_row_layout, parent, false);


      TextView noteNameTextView = (TextView) rowView.findViewById(R.id.noteNameTextView);
      TextView noteDateTextView = (TextView) rowView.findViewById(R.id.noteDateTextView);

      String dateNoSeconds = values.get(position).getLastChangeFormatter().substring(0, 5) + (values.get(position).getLastChangeFormatter().substring(8));

      noteNameTextView.setText(values.get(position).getTitle());
      noteDateTextView.setText(dateNoSeconds);

      return rowView;
   }


}
